package com.fenghuajueli.libbasecoreui.user;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.google.gson.reflect.TypeToken;
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants;
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.data.entity.user.UserInfoEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.regex.Pattern;

/**
 * 用户相关信息管理工具
 */
public class UserInfoUtils {

    public static final String USER_LOGIN_INFO = "USER_LOGIN_INFO";
    private static volatile UserInfoUtils instance;
    private UserInfoEntity userInfoEntity;

    public static UserInfoUtils getInstance() {
        if (instance == null) {
            synchronized (UserInfoUtils.class) {
                if (instance == null) {
                    instance = new UserInfoUtils();
                }
            }
        }
        return instance;
    }

    public void setUserInfoEntity(UserInfoEntity userInfoEntity) {
        if (userInfoEntity != null) {
            this.userInfoEntity = userInfoEntity;
        }
    }

    public UserInfoEntity getUserInfoEntity() {
        return userInfoEntity;
    }


    /**
     * 供外部调用 判断是否需要跳转去激活手机号和绑定手机号
     */
    public void checkOrBindPhone() {
        if (userInfoEntity != null) {
            if (isVip()) {
                if (!isActivePhone()) {
                    ARouter.getInstance().build(UserModuleRoute.USER_BIND_PHONE_ACTIVITY)
                            .withInt("type", isPhoneNum() ? 0 : 1)
                            .navigation();
                }
            }

        }
    }

    /**
     * 清除本地登录的信息
     */
    public void clearLoginInfo() {
        userInfoEntity = null;
        MmkvUtils.mmkv.encode(USER_LOGIN_INFO, "");
    }


    /**
     * 返回是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return userInfoEntity != null;
    }

    public boolean isVip() {
        if (userInfoEntity == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() > 100) {
            //自动订阅会员类型，这里需要根据天数来判断vip状态 status值表示的是否和支付宝解约 0表示解约了 1表示签约状态中
            return userInfoEntity.getUserInfo().getVip_info().getDays() > 0;
        } else {
            if (userInfoEntity.getUserInfo().getVip_info().getStatus() == 0) {
                return false;
            }
            return userInfoEntity.getUserInfo().getVip_info().getVip_type() > 0;
        }
    }

    /**
     * 是否激活或绑定了手机
     *
     * @return
     */
    public boolean isActivePhone() {
        if (userInfoEntity == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return false;
        }
        return userInfoEntity.getUserInfo().isPhone_active();
    }

    /**
     * 判断用户是否已经解除了vip的订阅 仅在viptype>100时使用
     *
     * @return true表示已经解约
     */
    public boolean isRemoveSubscribeVip() {
        if (userInfoEntity == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() > 100) {
            return userInfoEntity.getUserInfo().getVip_info().getStatus() == 0;
        } else {
            return true;
        }
    }


    /**
     * 根据当前的vip类型获取对应的会员类型文案
     *
     * @return
     */
    public String getVipNameByType() {
        if (userInfoEntity == null) {
            return "普通用户";
        }
        if (userInfoEntity.getUserInfo() == null) {
            return "普通用户";
        }
        String vipName = "普通用户";
        switch (userInfoEntity.getUserInfo().getVip_info().getVip_type()) {
            default:
                vipName = "普通用户";
                break;
            case 1:
                vipName = "永久会员";
                break;
            case 2:
                vipName = "年度会员";
                break;
            case 3:
                vipName = "半年度会员";
                break;
            case 4:
                vipName = "季度会员";
                break;
            case 5:
                vipName = "月度会员";
                break;
            case 6:
                vipName = "一周会员";
                break;
            case 101:
                if (getVipDaysByType() > 0) {
                    if (isRemoveSubscribeVip()) {
                        vipName = "连续包周会员(已取消订阅)";
                    } else {
                        vipName = "连续包周会员";
                    }
                } else {
                    vipName = "普通用户";
                }
                break;
            case 102:
                if (getVipDaysByType() > 0) {
                    if (isRemoveSubscribeVip()) {
                        vipName = "连续包月会员(已取消订阅)";
                    } else {
                        vipName = "连续包月会员";
                    }
                } else {
                    vipName = "普通用户";
                }
                break;
            case 103:
                if (getVipDaysByType() > 0) {
                    if (isRemoveSubscribeVip()) {
                        vipName = "连续包季会员(已取消订阅)";
                    } else {
                        vipName = "连续包季会员";
                    }
                } else {
                    vipName = "普通用户";
                }
                break;
            case 104:
                if (getVipDaysByType() > 0) {
                    if (isRemoveSubscribeVip()) {
                        vipName = "连续包年会员(已取消订阅)";
                    } else {
                        vipName = "连续包年会员";
                    }
                } else {
                    vipName = "普通用户";
                }
                break;
            case 1011:
                vipName = "";
        }
        return vipName;
    }

    /**
     * 根据当前的vip类型获取对应的会员显示天数
     *
     * @return
     */
    public String getShowVipDaysByType() {
        if (userInfoEntity == null) {
            return "0天";
        }
        if (userInfoEntity.getUserInfo() == null) {
            return "0天";
        }
        String vipDay = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getVip_info().getDays() + "天";
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() == 1) {
            vipDay = "无限";
        }
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() < 100) {
            if (userInfoEntity.getUserInfo().getVip_info().getStatus() == 0) {
                return "0天";
            }
        }
        return vipDay;
    }

    private int getVipDaysByType() {
        if (userInfoEntity == null) {
            return 0;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return 0;
        }
        int vipDay = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getVip_info().getDays();
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() == 1) {
            vipDay = 9999;
        }
        if (userInfoEntity.getUserInfo().getVip_info().getVip_type() < 100) {
            if (userInfoEntity.getUserInfo().getVip_info().getStatus() == 0) {
                return 0;
            }
        }
        return vipDay;
    }

    /**
     * 存储用户的信息到本地
     *
     * @param userInfoEntity
     */
    public void saveUserInfoToLocal(UserInfoEntity userInfoEntity) {
        if (userInfoEntity != null) {
            MmkvUtils.mmkv.encode(USER_LOGIN_INFO, GsonUtils.toJson(userInfoEntity));
        }
    }


    /**
     * 检测是否是永久VIP
     *
     * @return
     */
    public boolean isForeverVip() {
        if (userInfoEntity == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return false;
        }
        //先检测vip状态是否有效
        if (userInfoEntity.getUserInfo().getVip_info().getStatus() == 0) {
            return false;
        }
        return userInfoEntity.getUserInfo().getVip_info().getVip_type() == 1;
    }


    /**
     * 判断用户的手机号字段是否是真正的手机号  因为第三方登录手机号 字段是生成的字符串
     *
     * @return
     */
    public boolean isPhoneNum() {
        if (userInfoEntity == null) {
            return false;
        }
        if (userInfoEntity.getUserInfo() == null) {
            return false;
        }
        return isPhoneNumber(userInfoEntity.getUserInfo().getPhone());
    }

    /**
     * 判断一个字符串是否是数字。
     *
     * @param string
     * @return
     */
    public boolean isPhoneNumber(String string) {
        if (string == null)
            return false;
        String data = "^1[\\d]{10}";
        Pattern pattern = Pattern.compile(data);
        return pattern.matcher(string).matches();
    }


    /**
     * 执行本地登录
     */
    public void localLogin() {
        try {
            String userInfo = MmkvUtils.mmkv.decodeString(USER_LOGIN_INFO);
            if (!TextUtils.isEmpty(userInfo)) {
                LogUtils.d("本地存储的信息：" + userInfo);
                UserInfoEntity userInfoEntity = GsonUtils.getGson().fromJson(userInfo, new TypeToken<UserInfoEntity>() {
                }.getType());
                if (userInfoEntity != null) {
                    LogUtils.d("本地登录成功：" + userInfoEntity.getToken());
                    setUserInfoEntity(userInfoEntity);
                    EventBus.getDefault().post(new EventEntity(EventBusConstants.LOGIN_SUCCESS, userInfo));
                }
            } else {
                LogUtils.d("本地存储的信息为空-------》：");
            }
        } catch (Exception e) {
            LogUtils.d("本地登录失败。。。。");
        }
    }
}
